//
//  FMAttachmentBehaviorViewController.m
//  物理仿真
//
//  Created by Annie on 2020/5/27.
//  Copyright © 2020 Annie. All rights reserved.
//

#import "FMAttachmentBehaviorViewController.h"

@interface FMAttachmentBehaviorViewController ()
@property (nonatomic, strong) NSMutableArray<UIView *> *fm_views;
@property (nonatomic, strong) UIDynamicAnimator * animator;
@property (nonatomic, strong) UIGravityBehavior *gravity;
@property (nonatomic, strong) UICollisionBehavior *collision;

@property (nonatomic, strong) UIAttachmentBehavior *attachment;

@end

@implementation FMAttachmentBehaviorViewController

- (NSMutableArray<UIView *> *)fm_views{
    if (!_fm_views) {
        _fm_views = [NSMutableArray array];
    }
    return _fm_views;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    CGRect frame = CGRectMake(50, 300, 20, 20);
    for (NSInteger i = 0; i<9; i++) {
        UIView *view = [UIView new];
        view.backgroundColor = [UIColor redColor];
        [self.view addSubview:view];
        view.layer.cornerRadius = 10;
        view.layer.masksToBounds = YES;
        if (i == 8) {
            view.backgroundColor = [UIColor greenColor];
            view.layer.cornerRadius = 10 + 5;
            frame = CGRectMake(CGRectGetMaxX(frame) + 0, frame.origin.y, frame.size.width + 10, frame.size.height + 10);
            view.frame = frame;
        }else{
            frame = CGRectMake(CGRectGetMaxX(frame) + 0, frame.origin.y, frame.size.width, frame.size.height);
            view.frame = frame;
//            CGRectMake(100 + 20 * i, 300, 20, 20);
        }
        [self.fm_views addObject:view];
    }
    
    
    // 添加吸附吸附行为
    for (int i = 0; i < self.fm_views.count - 1; i++) {
        UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:self.fm_views[i] attachedToItem:self.fm_views[i+1]];
        [self.animator addBehavior:attachment];
    }
    
    [self addPanGestureWithView:self.fm_views.lastObject];
    
    [self.fm_views enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.gravity addItem:obj];
        [self.collision addItem:obj];
    }];
    
    [self.animator addBehavior:self.gravity];
    
    [self.animator addBehavior:self.collision];
    
}
- (UIDynamicAnimator *)animator{
    if (!_animator) {
        UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
        _animator = animator;
    }
    return _animator;
}
- (UIGravityBehavior *)gravity{
    if (!_gravity) {
        UIGravityBehavior *gravity = [UIGravityBehavior new];
        gravity.gravityDirection = CGVectorMake(0, 0.35);
        _gravity = gravity;
    }
    return _gravity;
}


- (UICollisionBehavior *)collision{
    if (!_collision) {
        UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
        collision.translatesReferenceBoundsIntoBoundary = YES;
        
        _collision = collision;
        
    }
    return _collision;
}


- (void)addPanGestureWithView:(UIView *)view{
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [view addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan{
    
    CGPoint loc = [pan locationInView:self.view];

    if (UIGestureRecognizerStateBegan == pan.state) {
        // 开始拖拽,实例化附加行为
        _attachment = [[UIAttachmentBehavior alloc] initWithItem:pan.view attachedToAnchor:loc];
        [_animator addBehavior:_attachment];

    } else if (UIGestureRecognizerStateChanged == pan.state) {
        // 拖拽过程中
        _attachment.anchorPoint = loc;

    } else if (UIGestureRecognizerStateEnded == pan.state){
        // 结束拖拽
        [_animator removeBehavior:_attachment];

    }
    
}


@end
